package com.tianji.learning.controller;


import com.tianji.api.dto.trade.OrderBasicDTO;
import com.tianji.common.domain.dto.PageDTO;
import com.tianji.common.domain.query.PageQuery;
import com.tianji.learning.domain.dto.LearningPlanDTO;
import com.tianji.learning.domain.vo.LearningLessonVO;
import com.tianji.learning.domain.vo.LearningPlanPageVO;
import com.tianji.learning.service.ILearningLessonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 学生课程表 前端控制器
 * </p>
 *
 * @author yp
 * @since 2023-09-05
 */
@RestController
@RequestMapping("/lessons")
@RequiredArgsConstructor
@Slf4j
@Api(tags = "课程相关接口")
public class LearningLessonController {

    private final ILearningLessonService iLearningLessonService;

    /**
     * 分页查询我的课表
     *
     * @param pageQuery
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询我的课表")
    public PageDTO<LearningLessonVO> pageList(PageQuery pageQuery) {
        return iLearningLessonService.pageList(pageQuery);
    }

    /**
     * 查询最近正在学习的课程
     *
     * @return
     */
    @GetMapping("/now")
    @ApiOperation("查询最近正在学习的课程")
    public LearningLessonVO queryCurrent() {
        return iLearningLessonService.queryCurrent();
    }

    /**
     * 根据id查询指定课程的学习状态
     *
     * @param courseId
     * @return
     */
    @GetMapping("/{courseId}")
    @ApiOperation("根据id查询指定课程的学习状态")
    public LearningLessonVO queryByCourseId(@PathVariable("courseId") Long courseId) {
        return iLearningLessonService.queryByCourseId(courseId);
    }

    /**
     * 检查课程是否有效
     *
     * @param courseId
     * @return
     */
    @GetMapping("/{courseId}/valid")
    @ApiOperation("检查课程是否有效")
    public Long qualification(@PathVariable("courseId") Long courseId) {
        return iLearningLessonService.qualification(courseId);
    }

    /**
     * 删除课表中的课程
     *
     * @param courseId
     */
    @DeleteMapping("/{courseId}")
    @ApiOperation("删除课表中的课程")
    public void deleteLearningLesson(@PathVariable("courseId") Long courseId) {
        iLearningLessonService.deleteLearningLesson(OrderBasicDTO.builder().courseIds(List.of(courseId)).build());
    }

    /**
     * 统计课程的学习人数
     *
     * @param courseId
     * @return
     */
    @GetMapping("/lessons/{courseId}/count")
    @ApiOperation("统计课程的学习人数")
    public Integer countPeople(@PathVariable("courseId") Long courseId) {
        return iLearningLessonService.countPeople(courseId);
    }

    /**
     * 创建学习计划
     *
     * @param dto
     */
    @PostMapping("/plans")
    @ApiOperation("创建学习计划")
    public void createLearningPlan(@RequestBody LearningPlanDTO dto) {
        iLearningLessonService.createLearningPlan(dto);
    }

    /**
     * 查询学习计划和进度
     *
     * @param pageQuery
     * @return
     */
    @ApiOperation("查询学习计划和进度")
    @GetMapping("/plans")
    public LearningPlanPageVO selectRecords(PageQuery pageQuery) {
        return iLearningLessonService.selectRecords(pageQuery);
    }
}
